package Java多线程技术.第3章.多生产一消费;

import java.util.ArrayList;
import java.util.List;

public class MyStack3 {
    private List list = new ArrayList();


    public synchronized void push(){
        try {
            //if(list.size() == 1)
            while (list.size() == 1){
                this.wait();
            }
            list.add("anyString"+Math.random());
            this.notifyAll();
            System.out.println("push size "+ list.size());
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }


    public synchronized String pop(){
        String value = "";
        try {
            //if(list.size() == 0)
            while (list.size() == 0){
                this.wait();
            }
            value = (String) list.get(0);
            list.remove(0);
            this.notifyAll();
            System.out.println("pop size "+list.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return value;

    }
}
